const connection = require('../app/database')

class UserService {

  // 将user存储到数据库
  async create(user) {
    try {
      const { nickname, password } = user
      // 用户表
      const statement = `insert into user (nickname, password) values (?,?)`  // 定义sql语句
      const result = await connection.execute(statement, [nickname, password])  // 执行sql语句

      // 用户详情表
      const statement2 = `insert into user_desc (userId) values (?)`
      // @ts-ignore
      await connection.execute(statement2, [result[0].insertId])

      // 用户数据表
      const statement3 = `insert into user_data (userId) values (?)`
      // @ts-ignore
      await connection.execute(statement3, [result[0].insertId])

      return result[0]  // 返回结果
    } catch (error) {
      console.log(error)
    }
  }

  // 验证登录
  async Authlogin(id, nickname) {
    try {
      const statement = `select * from user where id = ? and nickname = ?`
      const result = await connection.execute(statement, [id, nickname])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 根据用户名返回用户
  async getUserByNickname(nickname) {
    try {
      const statement = `select * from user where nickname = ?`
      const result = await connection.execute(statement, [nickname])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 返回用户数据
  async getUserData(userId) {
    try {
      const statement = `select * from user_data where userId = ?`
      const result = await connection.execute(statement, [userId])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 返回用户详情
  async getUserDesc(userId) {
    try {
      const statement = `select * from user_desc where userId = ?`
      const result = await connection.execute(statement, [userId])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 返回用户档案数据
  async getUserProfile(userId) {
    try {
      const statement = `SELECT
      JSON_OBJECT (
        'id',
        u.id,
        'nickname',
        u.nickname,
        'sex',
        ue.sex,
        'avatar',
        u.avatar,
        'wall',
        ue.wall,
        'createAt',
        u.createAt,
        'updateAt',
        u.updateAt
      ) 'user',
      JSON_OBJECT (
        'userId',
        ua.userId,
        'visitorNum',
        ua.visitorNum,
        'qiushiNum',
        ua.qiushiNum,
        'dianzanNum',
        ua.dianzanNum,
        'commentNum',
        ua.commentNum,
        'collectNum',
        ua.collectNum,
        'praiseNum',
        ua.praiseNum,
        'followNum',
        ua.followNum,
        'fensiNum',
        ua.fensiNum
      ) 'userData',
      JSON_OBJECT (
        'userId',
        ue.userId,
        'wall',
        ue.wall,
        'emotion',
        ue.emotion,
        'vocation',
        ue.vocation,
        'hometown',
        ue.hometown,
        'birthday',
        ue.birthday,
        'hobby',
        ue.hobby,
        'sign',
        ue.sign,
        'registerAt',
        ue.registerAt,
        'updateAt',
        ue.updateAt
      ) 'userDesc'
    FROM user u
    LEFT JOIN user_data ua ON ua.userId = u.id
    LEFT JOIN user_desc ue ON ue.userId = u.id
    WHERE u.id = ?`
      const result = await connection.execute(statement, [userId])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 验证用户密码
  async findUserByPassword(userId, password) {
    try {
      const statement = `select nickname from user where id = ? and password = ?`
      const result = await connection.execute(statement, [userId, password])
      return result[0][0]
    } catch (error) {
      console.log(error)
    }
  }

  // 修改用户密码
  async updatePassword(userId, password) {
    try {
      const statement = `update user set password = ? where id = ?`
      const result = await connection.execute(statement, [password, userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 保存用户头像路径
  async updateAvatarByUserId(avatarUrl, userId) {
    try {
      const statement = `update user set avatar = ? where id = ?`
      const result = await connection.execute(statement, [avatarUrl, userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 保存用户壁纸路径
  async updateWallByUserId(wallUrl, userId) {
    try {
      const statement = `update user_desc set wall = ? where userId = ?`
      const result = await connection.execute(statement, [wallUrl, userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 更新用户名
  async updateNickname(id, nickname) {
    try {
      const statement = `update user set nickname = ? where id = ?`
      const result = await connection.execute(statement, [nickname, id])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 更新用户详情
  async updateUserDesc(userId, userDesc) {
    try {
      const statement = `update user_desc set sex = ?, emotion = ?, vocation = ?, hometown = ?, hobby = ?, sign = ?, birthday = ? where userId = ?`
      const result = await connection.execute(statement, [userDesc.sex, userDesc.emotion, userDesc.vocation, userDesc.hometown, userDesc.hobby, userDesc.sign, userDesc.birthday, userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

  // 更新用户数据
  async updateUserData(userData) {
    try {
      const statement = `update user_data set ${userData.type}Num = ${userData.type}Num + 1 where userId = ?`
      const result = await connection.execute(statement, [userData.userId])
      return result[0]
    } catch (error) {
      console.log(error)
    }
  }

}

module.exports = new UserService()
